#include<iostream>
#include<unordered_map>

using namespace std;

class Solution {
public:
    int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
        int n=s.size();
        unordered_map<char,int> letters;
        unordered_map<string,int> words;
        string str;
        int ans=0;
        for(int left=0,right=0;right<n;++right)
        {
            str+=s[right];
            letters[s[right]]++;
            while(left<right&&(letters.size()>maxLetters||str.size()>minSize))
            {
                letters[s[left]]--;
                str=str.substr(1);
                if(letters[s[left]]==0) letters.erase(s[left]);
                ++left;
            }
            if(str.size()>=minSize) 
            {
                words[str]++;
                ans=max(ans,words[str]);
            }
        }
        return ans;
    }
};
